package com.nut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nut.common.Result;
import com.nut.entity.SysRole;
import com.nut.entity.SysRoleMenu;
import com.nut.entity.SysUser;
import com.nut.mapper.SysRoleMapper;
import com.nut.mapper.SysRoleMenuMapper;
import com.nut.mapper.SysUserMapper;
import com.nut.mapper.SysUserRoleMapper;
import com.nut.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public List<SysRole> queryAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public Result<IPage<SysRole>> list(String name, Integer pageNo, Integer pageSize) {
        Page<SysRole> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(name)) {
            queryWrapper.eq(SysRole::getName, name);
        }
        queryWrapper.orderByDesc(SysRole::getId);

        IPage<SysRole> list = baseMapper.selectPage(page, queryWrapper);
        return Result.ok(list);
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {

        baseMapper.deleteBatchIds(ids);

        for (Integer id : ids) {
            //删除角色关联的菜单
            sysRoleMenuMapper.delete(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id));
        }
        return Result.ok();
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public Result addRoleMenus(SysRole sysRole, List<Integer> menuIds) {
//
//        if (!StringUtils.hasLength(sysRole.getName())) {
//            return Result.fail("请输入角色名称");
//        }
//
//        baseMapper.insert(sysRole);
//
//        SysRole role = baseMapper.selectOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getName, sysRole.getName()));
//        for (Integer menuId : menuIds) {
//            SysRoleMenu sysRoleMenu = new SysRoleMenu();
//            sysRoleMenu.setMenuId(menuId);
//            sysRoleMenu.setRoleId(role.getId());
//            sysRoleMenuMapper.insert(sysRoleMenu);
//        }
//
//        return Result.ok();
//    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateRoleMenus(Integer roleId, List<Integer> menuIds) {

        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
        List<Integer> nowMenuIds = sysRoleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());

        List<SysRoleMenu> deleteMenus = sysRoleMenus.stream().filter(a -> !menuIds.contains(a.getMenuId())).collect(Collectors.toList());
        List<Integer> addMenuIds = menuIds.stream().filter(a -> !nowMenuIds.contains(a)).collect(Collectors.toList());

        for (SysRoleMenu deleteMenu : deleteMenus) {
            sysRoleMenuMapper.deleteById(deleteMenu.getId());
        }

        for (Integer addMenuId : addMenuIds) {
            SysRoleMenu menu = new SysRoleMenu();
            menu.setRoleId(roleId);
            menu.setMenuId(addMenuId);
            sysRoleMenuMapper.insert(menu);
        }
        return Result.ok();
    }

    @Override
    public List<Integer> getMenuIdsById(Integer id) {
        List<Integer> menuIds = sysRoleMenuMapper.selectMenuIds(id);
        return menuIds;
    }

    @Override
    public List<Integer> getRoleIdsByUserId(Integer userId) {
        List<Integer> roleIds = sysUserRoleMapper.selectByUserId(userId);
        return roleIds;
    }

    @Override
    public List<String> getPermissionByUserId(Integer userId) {

        SysUser sysUser = sysUserMapper.selectById(userId);
        if (sysUser == null) {
            return Collections.emptyList();
        }

        return null;
    }

    @Override
    public List<String> getPermissionByRoleId(Integer roleId) {


        return null;
    }

    private static void check2(List<Integer> nowList, List<Integer> updateList) {

        System.out.println(nowList);
        System.out.println(updateList);

        List<Integer> collect1 = nowList.stream().filter(a -> !updateList.contains(a)).collect(Collectors.toList());
        List<Integer> collect2 = updateList.stream().filter(a -> !nowList.contains(a)).collect(Collectors.toList());

        System.out.println(collect1);
        System.out.println(collect2);

    }

    public static void main(String[] args) {

        ArrayList<Integer> list = new ArrayList<>();
        list.add(5);
        list.add(9);
        list.add(1);
        list.add(3);
        list.add(8);
        list.add(6);

        ArrayList<Integer> nowList = new ArrayList<>();
        nowList.add(2);
        nowList.add(3);
        nowList.add(1);
        nowList.add(4);
        nowList.add(8);
        nowList.add(5);
        nowList.add(10);
        nowList.add(13);
        nowList.add(7);
        nowList.add(23);
        nowList.add(26);

//        Collections.sort(nowList);

//        check(list, nowList);
        check2(list, nowList);


    }
}
